package com.web.service;

import com.web.model.user;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import com.web.dao.UserDao;

/**
 * Created by Administrator on 2016/12/13.
 */
@Service
public class userService implements UserDetailsService {
    @Autowired
    private UserDao userDao;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        if (username.isEmpty()) {
            throw new BadCredentialsException("用户名不能为空!");
        }
        user user = null;
        try {
            user = userDao.selectByUsername(username);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if( user == null ){
            throw new BadCredentialsException("用户不存在!");
        }
        String password = user.getPassword();
//        password = MD5Util.getEncrypt(password);
        user.setPassword(password);
        try {
            String content = "用户"+username+"登录成功!";
            int syslogType = 4;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return user;
    }
}
